/*
 * MorseME.java
 *
 * Created on May 15, 2008, 2:07 AM
 */

package morseme;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.nokia.mid.sound.*;
import com.nokia.mid.ui.DeviceControl;
import java.io.*;


/**
 *
 * @author  tt
 * @version
 */
public class MorseME extends MIDlet {
private static MorseME morseME;
private SMSServer server;
public static int DEFAULT_PORT = 6535;
private static Sound sound;

public MorseME() {

try{

morseME = this;
String df = this.getAppProperty("Default-Port");
this.DEFAULT_PORT = Integer.parseInt(df);
server = new SMSServer(this.DEFAULT_PORT);

sound = new Sound(0, 1L);

}catch(Exception ex){
ex.printStackTrace();
}
}

protected void startApp() {
    
     

Display display = Display.getDisplay(this);
display.setCurrent(new MainMenu());
}

protected void pauseApp() {

}

protected void destroyApp(boolean unconditional) {
}

public static MorseME getMIDlet(){

return morseME;
}

private static void play(int frequency, long duration) {
try{
if(Option.SOUND_SEL == 0){
sound.init(frequency, duration);
sound.play(1);
}
}catch(Exception ex){
ex.printStackTrace();
}
}

private static void vibrate(int freq, long dur){
if(Option.VIBRATION_SEL == 0){

DeviceControl.startVibra(freq, dur);

}
}

public static void play(String morseMsg){

if(Option.SOUND_SEL == 1 && Option.VIBRATION_SEL == 1){
return;
}

//Play Start Sound
play(1397, 1000L);


try{
Thread.sleep(1000);
}catch(Exception ex){
ex.printStackTrace();
}

for(int i = 0; i < morseMsg.length(); i++){
int sleep = 0;
char c = morseMsg.charAt(i);
switch(c){

case '|':
sleep = 5000;
break;

case '-':
try {
Thread.sleep(1000);
}
catch (InterruptedException ex1) {
}
play(1047, 2000L);
sleep = 2500;
vibrate(100, 2000L);
break;

case '.':
try {
Thread.sleep(1000);
}
catch (InterruptedException ex1) {
}
play(1047, 250L);
sleep = 250;
vibrate(50, 250L);
break;

case ' ':
sleep = 3000;
break;
}
try{
Thread.sleep(sleep);

}catch(Exception ex){
stop();
}

}

}

public static void stop(){

try{

if (sound != null) {
sound.stop();
}
DeviceControl.stopVibra();

}catch(Exception ex){
System.out.println(")))))))))))4");
ex.printStackTrace();
}
}

public void quitApp(){

try {
if(server != null){
server.close();
}
}catch (IOException ex) {
ex.printStackTrace();
}

destroyApp(true);
notifyDestroyed();
}
}

